<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class SubMenu extends AppModel {
	public function findAllData(){
		return $this->find("all",array(
			'conditions' => array('deleted' => 0)
			));
	}
	
	public function findById($id=null){
		return $this->find('first',array(
			'conditions'=>array('id'=>$id)
			));
	}
	
	public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {    
        $recursive = -1;
        // Mandatory to have
        $this->useTable = false;
        $sql = '';
        $sql .= "SELECT sub_menus.id, sub_menus.name, sub_menus.short_link,menu_categories.category_name   from sub_menus 
					INNER JOIN menu_categories 
					on sub_menus.menu_category_id = menu_categories.id
					WHERE menu_categories.deleted = 0 and sub_menus.deleted = 0 LIMIT ";
        // Adding LIMIT Clause
        $sql .= (($page - 1) * $limit) . ', ' . $limit;
        $results = $this->query($sql);
        return $results;
    }
	public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $sql = '';
        $sql .= "SELECT sub_menus.id, sub_menus.name, sub_menus.short_link,menu_categories.category_name   from sub_menus 
					INNER JOIN menu_categories 
					on sub_menus.menu_category_id = menu_categories.id
					WHERE menu_categories.deleted = 0 and sub_menus.deleted = 0";
        $this->recursive = $recursive;
        $results = $this->query($sql);
        return count($results);
	 }
}
